package whereRU.frontEnd;

import whereRU.frontEnd.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This class is for creating the dialog that sets an alarm
 */
public class SetAlarmDialog extends DialogFragment {

	private final long courseId;
	private final boolean isSet;
	private final int alarmDelay;
	
	/** The activity that creates an instance of this dialog fragment must
     * 	implement this interface in order to receive event callbacks.
     * 	Each method passes the DialogFragment in case the host needs to query it. 
     */
    public interface SetAlarmDialogListener {
        public void onSetAlarmDialogPositiveClick(DialogFragment dialog, long courseId);
        public void onSetAlarmDialogNegativeClick(DialogFragment dialog, long courseId);
    }
    
    // Use this instance of the interface to deliver action events
    static SetAlarmDialogListener alarmDialogListener;
    
    public SetAlarmDialog(long courseId, boolean isSet, int alarmDelay) {
    	super();
    	this.courseId = courseId;
    	this.isSet = isSet;
    	this.alarmDelay = alarmDelay;
    }
    
    /**
     *  Call this to instantiate a new SetAlarmDialog.
     * @param activity  The activity hosting the dialog, which must implement the
     *                  SetAlarmDialogListener to receive event callbacks.
     * @returns A new instance of NoticeDialog.
     * @throws  ClassCastException if the host activity does not
     *          implement SetAlarmDialogListener
     */
    public static SetAlarmDialog newInstance(Activity activity, long courseId, 
    										boolean isSet, int alarmDelay) {
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events with it
            alarmDialogListener = (SetAlarmDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement SetAlarmDialogListener");
        }
        SetAlarmDialog dialog = new SetAlarmDialog(courseId, isSet, alarmDelay);
        return dialog;
    }
    
    /** 
     * Creates the dialog
     */
    @Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
    	AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		// Get the layout inflater
		LayoutInflater inflater = getActivity().getLayoutInflater();
		builder.setTitle("Alarm Options")
		// Inflate and set the layout for the dialog
		// Pass null as the parent view because its going in the dialog layout
		.setView(inflater.inflate(R.layout.dialog_set_alarm, null))
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// User clicked OK
				alarmDialogListener.onSetAlarmDialogPositiveClick(SetAlarmDialog.this, courseId);
			}
		})
		.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// User cancelled the dialog
				alarmDialogListener.onSetAlarmDialogNegativeClick(SetAlarmDialog.this, courseId);
			}
		});
	
		return builder.create();
    }
    
    /** 
     * Called when the activity is started 
     * Sets the checkbox and alarm delay values
     */
    @Override
    public void onStart() {
    	super.onStart();
    	Dialog d = getDialog();
    	Integer delay = (Integer) alarmDelay;
		
    	// set the checkbox to whether or not it has been set already
    	CheckBox checkbox = (CheckBox) d.findViewById(R.id.checkbox_alarm);
		if(isSet)
			checkbox.setChecked(true);
		
		// set the edit text field with the current alarm delay
		EditText etDelay = (EditText) d.findViewById(R.id.et_alarmDelay);
		etDelay.setText(delay.toString(), TextView.BufferType.EDITABLE);
    }
    
    /**
     * Returns the course id associated with this alarm
     * @return	the course id associated with this alarm
     */
    public long getCourseId() { return courseId; }
    
}